package demo1;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

//编写俄罗斯方块主类
public class Tetris extends JPanel {
    //声明正在下落的方块
    private Tetromino currenOne = Tetromino.randomOne();
    //声明将要下落的方块
    private Tetromino nextOne = Tetromino.randomOne();
    //声明游戏主区域
    private Cell[][] wall = new Cell[18][9];
    //声明单元格的值为多少像素
    private static final int CELL_SIZE = 44;
    //声明游戏分数池，当前获得游戏到粉丝，当前已消除的行数
    int[] scores_pool = {0,1,2,5,10};
    //当前获得游戏到粉丝
    private int totalScore = 0;
    //当前已消除到行数
    private int totalLine = 0;

    //声明游戏的三种状态：游戏中、暂停、游戏结束
    public static final int PLAYING = 0;
    public static final int PAUSE = 1;
    public static final int GAMEOVER = 2;
    //声明变量存放当前游戏状态值
    private int game_state;
    //声明一个数组，用来显示游戏状态
    String[] show_state = {"P[pause]","C[continue]","S[replay]"};



    //载入方块类图片
    public static BufferedImage I;
    public static BufferedImage J;
    public static BufferedImage L;
    public static BufferedImage O;
    public static BufferedImage S;
    public static BufferedImage T;
    public static BufferedImage Z;
    public static BufferedImage backImage;

    static {
        try {
            I = ImageIO.read(new File("src/images/1.png"));
            J = ImageIO.read(new File("src/images/2.png"));
            L = ImageIO.read(new File("src/images/3.png"));
            O = ImageIO.read(new File("src/images/4.png"));
            S = ImageIO.read(new File("src/images/5.png"));
            T = ImageIO.read(new File("src/images/6.png"));
            Z = ImageIO.read(new File("src/images/7.png"));
            backImage = ImageIO.read(new File("src/images/background.png"));

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
//绘制页面
    @Override
    public void paint(Graphics g) {
        g.drawImage(backImage,0,0,null);

        g.translate(18,19);

        //绘制游戏主区域
        printWall(g);
        //绘制正在下落的四方格
        paintCurrentOne(g);
        //绘制将要下落的四方格
        printNextOne(g);
        //绘制游戏得分
        paintScore(g);
        //绘制游戏状态
        paintState(g);
    }

    private void paintState(Graphics g) {
        if (game_state == PLAYING){
            g.drawString(show_state[PLAYING],470,620 );
        }else if(game_state == PAUSE) {
            g.drawString(show_state[PAUSE],470,620);
        }else if(game_state == GAMEOVER){
            g.drawString(show_state[GAMEOVER],470,620);
            g.setColor(Color.red);
            g.setFont(new Font(Font.DIALOG,Font.BOLD,60));
            g.drawString("游戏结束",69,450);
        }
    }

    private void paintScore(Graphics g) {
        g.setFont(new Font(Font.DIALOG_INPUT,Font.BOLD,30));
        g.drawString("总分数:"+totalScore,470,250);
        g.drawString("行数:"+ totalLine,470,430);
    }

    private void printNextOne(Graphics g) {
        Cell[] cells = nextOne.cells;
        for (Cell cell : cells) {
            int x = cell.getCol() * CELL_SIZE +346;
            int y = cell.getRow() * CELL_SIZE +25;
            g.drawImage(cell.getImage(),x,y,null);
        }
    }

    private void paintCurrentOne(Graphics g) {
        Cell[] cells = currenOne.cells;
        for (Cell cell : cells) {
            int x = cell.getCol() * CELL_SIZE;
            int y = cell.getRow() * CELL_SIZE;
            g.drawImage(cell.getImage(),x,y,null);
        }
    }

    private void printWall(Graphics g) {
        for (int i = 0; i < wall.length; i++) {
            for (int j = 0; j < wall[i].length; j++) {
                int x = j * CELL_SIZE;
                int y = i * CELL_SIZE;
                Cell cell = wall[i][j];
                //判断当前单元格是否有小方块，如果没有就绘制矩形，否则将小方块嵌入墙中
                if (cell == null){
                    g.drawRect(x,y,CELL_SIZE,CELL_SIZE);
                }else {
                    g.drawImage(cell.getImage(),x,y,null);
                }
            }
        }
    }
    //判断游戏是否出界
    public boolean outOfBounds(){
        Cell[] cells = currenOne.cells;
        for (Cell cell : cells) {
            int col = cell.getCol();
            int row = cell.getRow();
            if (row < 0 || row > wall.length-1 || col <0 || col>wall[0].length-1){
                return true;
            }
        }
        return false;
    }
    //判断方块是否重合
    public boolean coincide(){
        Cell[] cells = currenOne.cells;
        for (Cell cell : cells) {
            int row = cell.getRow();
            int col = cell.getCol();
            if (wall[row][col] != null){
                return true;
            }
        }
        return false;
    }
    //按键一次四方格左移一次
    public void moveLeftAction(){
        currenOne.moveLeft();
        //判断是否越界，或者四方格是否重合
        if (outOfBounds() || coincide()){
            currenOne.moveRight();
        }
    }
    //按键一次四方格右移一次
    public void moveRightAction(){
        currenOne.moveRight();
        if (outOfBounds() || coincide()){
            currenOne.moveLeft();
        }
    }

    public static void main(String[] args) {
        //创建一个窗口对象
        JFrame frame = new JFrame("俄罗斯方块");
        //创建游戏机界面，也就是面板
        Tetris panel = new Tetris();
        //将面板嵌入到窗口中
        frame.add(panel);
        //设置可见
        frame.setVisible(true);
        //窗口尺寸
        frame.setSize(730,879);
        //窗口居中
        frame.setLocationRelativeTo(null);
        //窗口关闭时程序终止
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


}
